package com.btpn.custody.web.m001.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import com.btpn.custody.dao.IRegion;
import com.btpn.custody.entity.Region;
import com.btpn.custody.service.IGlobalService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

@ParentPackage(value="custody")
@Results({
	@Result(name="input", location="t_region")
})
@Validations(
	requiredStrings = {
		@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "regionCode", message = "Region Code must be filled"),
		@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "regionName", message = "Region Name must be filled")
    }
)
public class RegionAction extends ActionSupport implements ServletRequestAware, Preparable {
	
	private static final long serialVersionUID = -443366108267208553L;
	
	@Autowired
	private IGlobalService globalService;
	
	@Autowired
	private IRegion regionDaoHibernate;
	
	private HttpServletRequest request;

	private Long regionId;
	private String regionCode;
	private String regionName;
	
	private Long _chk[];
	private List<Region> regions;
	
	@SkipValidation
	@Action(value = "/listRegion", results = { @Result(name="success", location="t_region") })
	public String execute() throws Exception {
				
		setRegionId(0L);
		setRegionCode("");
		setRegionName("");
		
		return SUCCESS;
	}
	
	@SkipValidation
	@Action(value = "/editRegion", results = { 
		@Result(name="success", location="t_region"),
		@Result(name="error", type="redirectAction", params = {"actionName", "listRegion"})
	})
	public String editRegion() throws Exception {
		
		String id = getRequest().getParameter("id");
		if (id != null) {
			Region editRegion = regionDaoHibernate.get(Long.parseLong(id));
			setRegionId(editRegion.getId());
			setRegionCode(editRegion.getCode());
			setRegionName(editRegion.getName());
			
			return SUCCESS;
		} else {
			return ERROR;
		}		
	}
	
	@Action(value = "/addOrUpdateRegion", results = { 
		@Result(name="success", type="redirectAction", params = {"actionName", "listRegion"})
	})
	public String addOrUpdateRegion() throws Exception {

		if (regionId == 0L) { 			// create : with validation
			// validate region code
			Region theRegion = regionDaoHibernate.getByCode(regionCode);
			if (theRegion != null) {
				addFieldError("regionCode", "Region Code must be unique");
				return INPUT;
			}
			
			// validate region name
			theRegion = regionDaoHibernate.getByName(regionName);
			if (theRegion != null) {
				addFieldError("regionName", "Region Name have been exist");			
				return INPUT;
			}
		}
		
		globalService.createOrUpdateRegion(regionId, regionCode, regionName, SecurityContextHolder.getContext().getAuthentication().getName());
		
		return SUCCESS;
	}

	@SkipValidation
	@Action(value = "/removeRegions", results = { 
			@Result(name="success", type="redirectAction", params = {"actionName", "listRegion"} )
	})
	public String removeRegions() throws Exception {
		
		globalService.deleteRegions(_chk, SecurityContextHolder.getContext().getAuthentication().getName());
		
		return SUCCESS;
	}
	
	@Override
	public void prepare() throws Exception {
		regions = regionDaoHibernate.getAll();
	}

	public List<Region> getRegions() {
		return regions;
	}

	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}

	public Long getRegionId() {
		return regionId;
	}

	public void setRegionId(Long regionId) {
		this.regionId = regionId;
	}

	public String getRegionCode() {
		return regionCode;
	}

	public void setRegionCode(String regionCode) {
		this.regionCode = regionCode;
	}

	public String getRegionName() {
		return regionName;
	}

	public void setRegionName(String regionName) {
		this.regionName = regionName;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public Long[] get_chk() {
		return _chk;
	}

	public void set_chk(Long[] _chk) {
		this._chk = _chk;
	}	
}
